package com.example.contentprovider0216.provide;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.contentprovider0216.db.UserDatabaseHelper;
import com.example.contentprovider0216.utils.Constants;

public class UserProvide extends ContentProvider {

    private UserDatabaseHelper mUserDatabaseHelper=null;

    private static final int USER_MATCH_CODE=1;

    private static UriMatcher sUriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
    static {
        sUriMatcher.addURI("com.example.contentprovider0216",null,USER_MATCH_CODE);
    }

    @Override
    public boolean onCreate() {
        mUserDatabaseHelper=new UserDatabaseHelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
                        @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        int result=sUriMatcher.match(uri);
        if (result==USER_MATCH_CODE){
            //匹配规则
            SQLiteDatabase db=mUserDatabaseHelper.getReadableDatabase();
            Cursor cursor=db.query(Constants.TABLE_NAME,projection,selection,selectionArgs,
                    null,null,sortOrder);
            return cursor;
        }else {
            //不匹配规则
            throw new IllegalArgumentException("参数错误");

        }

    }

    /**
     * 形式一、单条记录
     * vnd.android.cursor.item/自定义
     * 形式二、多条记录
     * vnd.android.cursor.dir/自定义
     * vnd:表示父类型和子类型具有非标准的、特定的形式。
     * 父类型已固定好(即不能更改),只能区别是单条还是多条记录
     * 子类型可自定义
     * @param uri
     * @return
     */
    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }
}
